<!DOCTYPE html>
<html lang="en">
  <head>
    <title>CoScope Class Reference</title>
    <link rel="stylesheet" type="text/css" href="../css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="../css/highlight.css" />
    <meta charset='utf-8'>
    <script src="../js/jquery.min.js" defer></script>
    <script src="../js/jazzy.js" defer></script>
    
    <script src="../js/lunr.min.js" defer></script>
    <script src="../js/typeahead.jquery.js" defer></script>
    <script src="../js/jazzy.search.js" defer></script>
  </head>
  <body>
    <a name="//apple_ref/swift/Class/CoScope" class="dashAnchor"></a>
    <a title="CoScope Class Reference"></a>
    <header>
      <div class="content-wrapper">
        <p><a href="../index.html">SwiftCoroutine 2.1.9 Docs</a> (88% documented)</p>
        <p class="header-right">
          <form role="search" action="../search.json">
            <input type="text" placeholder="Search documentation" data-typeahead>
          </form>
        </p>
      </div>
    </header>
    <div class="content-wrapper">
      <p id="breadcrumbs">
        <a href="../index.html">SwiftCoroutine Reference</a>
        <img id="carat" src="../img/carat.png" />
        CoScope Class Reference
      </p>
    </div>
    <div class="content-wrapper">
      <nav class="sidebar">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a href="../Classes.html">Classes</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Classes/CoChannel.html">CoChannel</a>
              </li>
              <li class="nav-group-task">
                <a href="../Classes/CoChannel/BufferType.html">– BufferType</a>
              </li>
              <li class="nav-group-task">
                <a href="../Classes/CoChannel/Receiver.html">– Receiver</a>
              </li>
              <li class="nav-group-task">
                <a href="../Classes/CoChannel/Sender.html">– Sender</a>
              </li>
              <li class="nav-group-task">
                <a href="../Classes/CoFuture.html">CoFuture</a>
              </li>
              <li class="nav-group-task">
                <a href="../Classes/CoPromise.html">CoPromise</a>
              </li>
              <li class="nav-group-task">
                <a href="../Classes/CoScope.html">CoScope</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="../Enums.html">Enumerations</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Enums/CoChannelError.html">CoChannelError</a>
              </li>
              <li class="nav-group-task">
                <a href="../Enums/CoFutureError.html">CoFutureError</a>
              </li>
              <li class="nav-group-task">
                <a href="../Enums/CoroutineError.html">CoroutineError</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="../Extensions.html">Extensions</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Extensions/DispatchQueue.html">DispatchQueue</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/Publisher.html">Publisher</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/URLSession.html">URLSession</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="../Protocols.html">Protocols</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Protocols/CoCancellable.html">CoCancellable</a>
              </li>
              <li class="nav-group-task">
                <a href="../Protocols/CoroutineScheduler.html">CoroutineScheduler</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="../Structs.html">Structures</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Structs/Coroutine.html">Coroutine</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">
        <section>
          <section class="section">
            <h1>CoScope</h1>
              <div class="declaration">
                <div class="language">
                  
                  <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">final</span> <span class="kd">class</span> <span class="kt">CoScope</span></code></pre>
<pre class="highlight swift"><code><span class="kd">extension</span> <span class="kt">CoScope</span><span class="p">:</span> <span class="kt"><a href="../Protocols/CoCancellable.html">CoCancellable</a></span></code></pre>

                </div>
              </div>
            <p>The holder of uncompleted <code><a href="../Protocols/CoCancellable.html">CoCancellable</a></code> and coroutines.</p>

<p><code>CoScope</code> helps to manage lifecycle of coroutines and <code><a href="../Protocols/CoCancellable.html">CoCancellable</a></code>, like <code><a href="../Classes/CoFuture.html">CoFuture</a></code> and <code><a href="../Classes/CoChannel.html">CoChannel</a></code>.
It keeps weak references on inner objects and cancels them on <code>cancel()</code> or deinit.
All completed objects are automaticaly removed from scope.</p>
<div class="aside aside-note">
    <p class="aside-title">Note</p>
    <code>CoScope</code> keeps weak references.

</div>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">scope</span> <span class="o">=</span> <span class="kt">CoScope</span><span class="p">()</span>

<span class="k">let</span> <span class="nv">future</span> <span class="o">=</span> <span class="nf">makeSomeFuture</span><span class="p">()</span><span class="o">.</span><span class="nf">added</span><span class="p">(</span><span class="nv">to</span><span class="p">:</span> <span class="n">scope</span><span class="p">)</span>

<span class="n">queue</span><span class="o">.</span><span class="nf">startCoroutine</span><span class="p">(</span><span class="nv">in</span><span class="p">:</span> <span class="n">scope</span><span class="p">)</span> <span class="p">{</span>
    <span class="o">.</span> <span class="o">.</span> <span class="o">.</span> <span class="kd">some</span> <span class="n">code</span> <span class="o">.</span> <span class="o">.</span> <span class="o">.</span>
    <span class="k">let</span> <span class="nv">result</span> <span class="o">=</span> <span class="k">try</span> <span class="n">future</span><span class="o">.</span><span class="nf">await</span><span class="p">()</span>
    <span class="o">.</span> <span class="o">.</span> <span class="o">.</span> <span class="kd">some</span> <span class="n">code</span> <span class="o">.</span> <span class="o">.</span> <span class="o">.</span>
<span class="p">}</span>

<span class="k">let</span> <span class="nv">future2</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">coroutineFuture</span> <span class="p">{</span>
    <span class="k">try</span> <span class="kt">Coroutine</span><span class="o">.</span><span class="nf">delay</span><span class="p">(</span><span class="o">.</span><span class="nf">seconds</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span> <span class="c1">// imitate some work</span>
    <span class="k">return</span> <span class="mi">5</span>
<span class="p">}</span><span class="o">.</span><span class="nf">added</span><span class="p">(</span><span class="nv">to</span><span class="p">:</span> <span class="n">scope</span><span class="p">)</span>

<span class="c1">//cancel all added futures and coroutines</span>
<span class="n">scope</span><span class="o">.</span><span class="nf">cancel</span><span class="p">()</span>
</code></pre>

          </section>
          <section class="section task-group-section">
            <div class="task-group">
              <ul>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:14SwiftCoroutine7CoScopeCACycfc"></a>
                    <a name="//apple_ref/swift/Method/init()" class="dashAnchor"></a>
                    <a class="token" href="#/s:14SwiftCoroutine7CoScopeCACycfc">init()</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Initializes a scope.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="nf">init</span><span class="p">()</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:14SwiftCoroutine7CoScopeC3addyyAA0C11Cancellable_pF"></a>
                    <a name="//apple_ref/swift/Method/add(_:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:14SwiftCoroutine7CoScopeC3addyyAA0C11Cancellable_pF">add(_:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Adds weak referance of <code><a href="../Protocols/CoCancellable.html">CoCancellable</a></code> to be canceled when the scope is being canceled or deinited.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">func</span> <span class="nf">add</span><span class="p">(</span><span class="n">_</span> <span class="nv">item</span><span class="p">:</span> <span class="kt"><a href="../Protocols/CoCancellable.html">CoCancellable</a></span><span class="p">)</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>item</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p><code><a href="../Protocols/CoCancellable.html">CoCancellable</a></code> to add.</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:14SwiftCoroutine7CoScopeC7isEmptySbvp"></a>
                    <a name="//apple_ref/swift/Property/isEmpty" class="dashAnchor"></a>
                    <a class="token" href="#/s:14SwiftCoroutine7CoScopeC7isEmptySbvp">isEmpty</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Returns <code>true</code> if the scope is empty (contains no <code><a href="../Protocols/CoCancellable.html">CoCancellable</a></code>).</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="k">var</span> <span class="nv">isEmpty</span><span class="p">:</span> <span class="kt">Bool</span> <span class="p">{</span> <span class="k">get</span> <span class="p">}</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
              </ul>
            </div>
            <div class="task-group">
              <div class="task-name-container">
                <a name="/cancel"></a>
                <a name="//apple_ref/swift/Section/cancel" class="dashAnchor"></a>
                <div class="section-name-container">
                  <a class="section-name-link" href="#/cancel"></a>
                  <h3 class="section-name"><p>cancel</p>
</h3>
                </div>
              </div>
              <ul>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:14SwiftCoroutine7CoScopeC10isCanceledSbvp"></a>
                    <a name="//apple_ref/swift/Property/isCanceled" class="dashAnchor"></a>
                    <a class="token" href="#/s:14SwiftCoroutine7CoScopeC10isCanceledSbvp">isCanceled</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Returns <code>true</code> if the scope is canceled.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="k">var</span> <span class="nv">isCanceled</span><span class="p">:</span> <span class="kt">Bool</span> <span class="p">{</span> <span class="k">get</span> <span class="p">}</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:14SwiftCoroutine7CoScopeC6cancelyyF"></a>
                    <a name="//apple_ref/swift/Method/cancel()" class="dashAnchor"></a>
                    <a class="token" href="#/s:14SwiftCoroutine7CoScopeC6cancelyyF">cancel()</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Cancels the scope and all <code><a href="../Protocols/CoCancellable.html">CoCancellable</a></code> that it contains.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">func</span> <span class="nf">cancel</span><span class="p">()</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:14SwiftCoroutine7CoScopeC12whenCompleteyyyycF"></a>
                    <a name="//apple_ref/swift/Method/whenComplete(_:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:14SwiftCoroutine7CoScopeC12whenCompleteyyyycF">whenComplete(_:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Adds an observer callback that is called when the <code>CoScope</code> is canceled or deinited.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">func</span> <span class="nf">whenComplete</span><span class="p">(</span><span class="n">_</span> <span class="nv">callback</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>callback</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The callback that is called when the scope is canceled or deinited.</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
              </ul>
            </div>
          </section>
        </section>
        <section id="footer">
          <p>&copy; 2020 <a class="link" href="https://github.com/belozierov/SwiftCoroutine" target="_blank" rel="external">Alex Belozierov</a>. All rights reserved. (Last updated: 2020-07-11)</p>
          <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.13.5</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
        </section>
      </article>
    </div>
  </body>
</div>
</html>
